package com.softwaredesign.springboot.mapper;

import com.softwaredesign.springboot.controller.dto.UserDTO;
import com.softwaredesign.springboot.controller.dto.UserRDTO;
import com.softwaredesign.springboot.entity.User;
import com.softwaredesign.springboot.entity.UserRole;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select * from sys_user limit #{pageNum}, #{pageSize}")
    List<User> findAll(Integer pageNum, Integer pageSize);

    @Select("select count(*) from sys_user")
    Integer findAllTotal();

    @Insert("INSERT into sys_user(username, password, nickname, email, phone, address)" +
            " VALUES (#{username}, #{password}, #{nickname}, #{email}, #{phone}, #{address})")
    int insert(User user);

    int update(User user);

    @Delete("delete from sys_user where id = #{id}")
    Integer deleteById(@Param("id") Integer id);

    @Select("select * from sys_user " +
            "where (username like #{username}) " +
            "and (nickname like #{nickname})" +
            "and (email like #{email}) " +
            "and (phone like #{phone})" +
            "and (address like #{address}) " +
            "limit #{pageNum}, #{pageSize}")
    List<User> selectPage(Integer pageNum,
                          Integer pageSize,
                          String username,
                          String nickname,
                          String email,
                          String phone,
                          String address);

    @Select("select count(*) from sys_user " +
            "where (username like #{username}) " +
            "and (nickname like #{nickname})" +
            "and (email like #{email}) " +
            "and (phone like #{phone})" +
            "and (address like #{address})")
    Integer selectTotal(String username,
                        String nickname,
                        String email,
                        String phone,
                        String address);

    @Select("select * from sys_user where username = #{username} and password = #{password}")
    List<UserDTO> login(String username,
                     String password);


    @Select("select count(*) from sys_user where username like #{username}")
    Integer findUserNameOfNewUser(String username);

    @Insert("INSERT into sys_user(username, password, nickname, email, phone, address)" +
            " VALUES (#{username}, #{password}, #{nickname}, #{email}, #{phone}, #{address})")
    boolean insertRDTO(UserRDTO userRDTO);

    @Select("select * from sys_user where id = #{id}")
    User findPersonalInfoByID(Integer id);

    @Select("select roleID from user_role where userID = #{id}")
    List<Integer> findUserRolesByID(Integer id);

    @Select("select roleCode from sys_role where id = #{id}")
    Integer findRoleCodeByID(Integer id);

    @Select("select authorityID from role_authority where roleID = #{id}")
    List<Integer> findRoleAuthoritiesByID(Integer id);

    @Select("select authorityCode from sys_authority where id = #{id}")
    Integer findAuthorityCodeByID(Integer id);

    @Select("select authorityName from sys_authority where id = #{id}")
    String findAuthorityNameByID(Integer id);

    @Update("update sys_user " +
            "set " +
            "username = #{username}, " +
            "password = #{password}, " +
            "nickname = #{nickname}, " +
            "email = #{email}, " +
            "phone = #{phone}, " +
            "address = #{address} " +
            "where id = #{id}")
    boolean updateAll(UserDTO userDTO);

    @Select("select id from sys_user")
    List<Integer> findAllUserIDs();

    @Select("select username from sys_user where id = #{id}")
    String findUserNameByID(Integer id);

    @Select("select roleName from sys_role where id = #{id}")
    String findRoleNameByID(Integer id);

    @Select("select id from sys_user where username like #{username}")
    List<Integer> findAllUserIDsByUserName(String username);

    @Select("select count(*) from user_role where userID = #{userID} and roleID = #{roleID}")
    Integer userRoleCount(Integer userID, Integer roleID);

    @Select("select count(*) from sys_role where id = #{id} and roleCode = #{roleCode}")
    Integer roleCodeCount(Integer id, Integer roleCode);

    @Select("select roleName from sys_role where id = #{id} and roleName like #{roleName}")
    String findRoleNameByIDAndSubName(Integer id, String roleName);

    @Select("select username from sys_user where id = #{id} and username like #{username}")
    String findUserNameByIDAndName(Integer id, String username);

    @Update("update user_role " +
            "set " +
            "roleID = #{roleID} " +
            "where userID = #{userID} and roleID = #{preRoleID}")
    boolean updateUserRole(UserRole userRole);

    @Insert("INSERT into user_role(userID, roleID)" +
            " VALUES (#{userID}, #{roleID})")
    boolean insertUserRole(UserRole userRole);

    @Select("select * from user_role where userID = #{userID} and roleID = #{roleID}")
    Integer ifExitUserRole(UserRole userRole);

    @Delete("delete from user_role where userID = #{userID} and roleID = #{roleID}")
    Integer deleteUserRole(Integer userID, Integer roleID);

}
